package base.thread;

public class SynchronizedStaticMethodDemo {

    //而修饰静态方法时，锁是类锁，也就是类名.class。
    //相当于  synchronized(SynchronizedStaticMethodDemo.this)  在此调用
    public synchronized static void method01(){
        int i = 5;
        while (i-- > 0) {
            System.out.println("method01           " + Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void method02() {

//        synchronized (SynchronizedStaticMethodDemo.class)  //当
        synchronized (this)
        {
            int i = 5;
            while (i-- > 0) {
                System.out.println("method02               " + Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                }
            }
        }
    }

    public static void main(String[] args) {
        final SynchronizedStaticMethodDemo myt2 = new SynchronizedStaticMethodDemo();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                myt2.method01();
            }
        }, "t1");
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                myt2.method02();
            }
        }, "t2");
        t2.start();
        t1.start();

        try {
            t2.join();
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


}
